package com.dataart.training.crimequalification.beans;

import org.springframework.context.annotation.Scope;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import java.util.Map;

@ManagedBean
@Scope("request")
public class ErrorBean {

    //-----------------------------------------------------------------------------

    private String errorMessage = "No error occured.";

    //-----------------------------------------------------------------------------

    public static final String ERROR_MESSAGE_KEY = "errormsg";

    //-----------------------------------------------------------------------------

    @PostConstruct
    private void init() {
        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        if (sessionMap.containsKey(ERROR_MESSAGE_KEY)) {
            errorMessage = (String) sessionMap.get(ERROR_MESSAGE_KEY);
            sessionMap.remove(ERROR_MESSAGE_KEY);
        }
    }

    //-----------------------------------------------------------------------------

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
